/**
 * Copyright (C) 2011-2012 Luca Li Greci
 *
 * This file is part of pojo-creator
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pojocreator.provider.number;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.math.RandomUtils;

/**
 * This class use an internal map of datatype/Function to associate a specific
 * sub class of Number with a Function. The private inner class Function acts as
 * wrapper of the random functionality available in {@link RandomUtils}.
 * <p>
 * This class does not need to be expose outside so the level package is set to
 * "default"
 * 
 * @author Luca Li Greci
 */
class RandomNumberGenerator {
    /**
     * Store the mapping between a Class type and the Function Object to call to
     * generate the data.
     */
    private Map < Class < ? >, Function < ? extends Number > > randomNumericMap;

    RandomNumberGenerator() {
        randomNumericMap = new HashMap < Class < ? >, Function < ? > >();
        randomNumericMap.put(Integer.class, new IntegerFunction());
        randomNumericMap.put(Number.class, new NumberFunction());
        randomNumericMap.put(BigInteger.class, new BigIntegerFunction());
        randomNumericMap.put(Long.class, new LongFunction());
        randomNumericMap.put(BigDecimal.class, new BigDecimalFunction());
        randomNumericMap.put(Byte.class, new ByteFunction());
        randomNumericMap.put(Double.class, new DoubleFunction());
        randomNumericMap.put(Float.class, new FloatFunction());
    }

    Number generate(Class < ? > type) {
        Function < ? extends Number > function = randomNumericMap.get(type);
        if (function != null) {
            return function.call();
        }
        return null;
    }

    /*
     * The following static final inner class have been created to fix findbugs
     * violation. A Performance issue with the key
     * (SIC_INNER_SHOULD_BE_STATIC_ANON)
     */

    /**
     * Functor to generate {@link Number}
     */
    private static final class NumberFunction implements Function < Number > {
        /**
         * @return a random {@link Integer} since {@link Number} is an abstract
         *         class.
         */
        @Override
        public Number call() {
            return Integer.valueOf(RandomUtils.nextInt());
        }
    }

    /**
     * Functor to generate {@link BigInteger}
     */
    private static final class BigIntegerFunction implements Function < BigInteger > {
        /**
         * @return a random {@link BigInteger}
         */
        @Override
        public BigInteger call() {
            return BigInteger.valueOf(RandomUtils.nextInt());
        }
    }

    /**
     * Functor to generate {@link Integer}
     */
    private static final class IntegerFunction implements Function < Integer > {
        /**
         * @return a random {@link Integer}
         */
        @Override
        public Integer call() {
            return Integer.valueOf(RandomUtils.nextInt());
        }
    }

    /**
     * Functor to generate {@link Long}
     */
    private static final class LongFunction implements Function < Long > {
        /**
         * @return a random {@link Long}
         */
        @Override
        public Long call() {
            return Long.valueOf(RandomUtils.nextLong());
        }
    }

    /**
     * Functor to generate {@link BigDecimal}
     */
    private static final class BigDecimalFunction implements Function < BigDecimal > {
        /**
         * @return a random {@link BigDecimal}
         */
        @Override
        public BigDecimal call() {
            return BigDecimal.valueOf(RandomUtils.nextLong());
        }
    }

    /**
     * Functor to generate {@link Byte}
     */
    private static final class ByteFunction implements Function < Byte > {
        /**
         * @return a random {@link Byte}
         */
        @Override
        public Byte call() {
            return Byte.valueOf((byte) RandomUtils.nextInt());
        }
    }

    /**
     * Functor to generate {@link Double}
     */
    private static final class DoubleFunction implements Function < Double > {
        /**
         * @return a random {@link Double}
         */
        @Override
        public Double call() {
            return Double.valueOf(RandomUtils.nextDouble());
        }
    }

    /**
     * Functor to generate {@link Float}
     */
    private static final class FloatFunction implements Function < Float > {
        /**
         * @return a random {@link Float}
         */
        @Override
        public Float call() {
            return Float.valueOf(RandomUtils.nextFloat());
        }
    }

    /**
     * Provide a generic Function that is implemented by the anonymous inner
     * classes. Each class will wrap the method invocation to one of the {@link
     * RandomUtils#} method
     * 
     * @author Luca Li Greci
     * 
     * @param <R> Returned value for a function
     */
    private interface Function < R extends Number > {
        /**
         * @return an implementation of Number.
         */
        R call();
    }
}
